"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongodbClient = void 0;
const Logger_1 = require("./Logger");
const MongoClient = require('mongodb').MongoClient;
// const mongo = require('mongodb');
class MongodbClient {
    static create(database, host, port, user, pass) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => {
                let client = new MongodbClient();
                client.database = database;
                client.host = host;
                client.port = port;
                client.user = user;
                client.pass = pass;
                let url = 'mongodb://' + host + ":" + port;
                MongoClient.connect(url, (err, db_client) => {
                    if (err) {
                        Logger_1.Logger.error("MongoClient connect error", err);
                        return;
                    }
                    const db = db_client.db(database);
                    // client.close();
                    client.db = db;
                    resolve(client);
                });
            });
        });
    }
    count(table_name) {
        return new Promise((resolve, reject) => {
            this.count_callback(table_name, (err, count) => {
                resolve(count);
            });
        });
    }
    count_callback(table_name, callback) {
        this.db.collection(table_name).countDocuments((err, count) => {
            if (err) {
                Logger_1.Logger.error("count error", err);
                callback && callback(err, 0);
                return;
            }
            Logger_1.Logger.log("count_callback suc==", count);
            callback && callback(err, count);
        });
        // this.db.collection(table_name).find().count((err, results)=>{
        //     if (err) {
        //         Logger.error("count error", err)
        //         callback && callback(err, [], [])
        //         return;
        //     }
        //     Logger.log("count_callback suc==", results);
        //     callback && callback(err, results)
        // })
    }
    queryPromise(table_name, whereObj = {}) {
        return new Promise((resolve, reject) => {
            this.query_callback(table_name, whereObj, (err, results) => {
                resolve(results);
            });
        });
    }
    query_callback(table_name, whereObj = {}, callback = null) {
        this.db.collection(table_name).find(whereObj).toArray((err, results) => {
            if (err) {
                Logger_1.Logger.error("query error", err);
                callback && callback(err);
                return;
            }
            // Logger.log("query suc==", results);
            callback && callback(err, results);
        });
    }
    createInex(table_name, indexName) {
        // this.db.collection(table_name).indexInformation((err, results)=>{
        //     Logger.log("indexInformation==err=", err)
        //     Logger.log("indexInformation=results==",results)
        // })
        this.db.collection(table_name).indexExists(indexName + "_1", (err, results) => {
            // Logger.log("indexExists==err=", err)
            // Logger.log("indexExists=results==", results)
            if (!results) {
                this.db.collection(table_name).createIndex(indexName, (err, results) => {
                    // Logger.log("createInex==err=", err)
                    // Logger.log("createInex=results==", results)
                });
            }
        });
    }
    createIndex_callback() {
    }
    insertOne(table_name, paramObj = {}) {
        return new Promise((resolve, reject) => {
            this.insertOne_callback(table_name, paramObj, (err, results) => {
                if (!err) {
                    resolve(results);
                }
                else {
                    resolve(null);
                }
            });
        });
    }
    insertOne_callback(table_name, paramObj = {}, callback = null) {
        this.db.collection(table_name).insertOne(paramObj, (err, results) => {
            if (err) {
                // Logger.error("insertOne error", err)
                callback && callback(err);
                return;
            }
            // Logger.log("insertOne suc==", results);
            callback && callback(err, results);
        });
    }
    updateOne(table_name, whereObj = {}, updateObj = {}) {
        return new Promise((resolve, reject) => {
            this.updateOne_callback(table_name, whereObj, updateObj, (err, results, fields) => {
                resolve(results);
            });
        });
    }
    updateOne_callback(table_name, whereObj = {}, updateObj = {}, callback = null) {
        let realUpdateObj = { $set: updateObj };
        this.db.collection(table_name).updateOne(whereObj, realUpdateObj, (err, results) => {
            if (err) {
                Logger_1.Logger.error("updateOne error", err);
                callback && callback(err);
                return;
            }
            // Logger.log("updateOne suc==", results);
            callback && callback(err, results);
        });
    }
}
exports.MongodbClient = MongodbClient;
